import {takeEvery, select, call, put, take} from 'redux-saga/effects'
import axios from 'axios'
import { message } from 'antd';
import { duration } from 'moment';

// 引入自定义操作
import { 
    ChangeMasterTasksActivate, 
    DeleteMasterTaskSaga, 
    DownLoadTaskEviSaga, 
    MasterChangeWorkerTaskSaga, 
    UpdateMasterTaskSaga,

    ChangeMasterTasksDataActivate
} from '../actions/taskMasterManagePage';

import { ChangeSpinACtivate } from '../actions/public'

// 引入常量
import { 
    GETMASTERTASKSSAGA, 
    MASTERCHANGEWORKERTASKSAGA, 
    DELETEMASTERTASKSAGA, 
    DOWNLOADTASKEVISAGA,
    UPDATEMASTERTASKSAGA
} from '../constants'
import { API_URL } from '../../env';

// 雇主管理任务页面
function* getMasterTasksSaga(action) {
    try{
        const res = yield call(axios.post, API_URL + "master_manage/master_tasks", {...action.data})
        if (res.status == 200){
            yield put(ChangeMasterTasksActivate({task_data: res.data.task_data}))
        } else {
            message.error('请求失败')
        }
        console.log("getMasterTasksSaga is ", res)
    } catch(e) {
        message.error('请求失败')
    }
}

// master用户修改用户开发状态
function* masterChangeWorkerTaskSaga(action) {
    
    const obj = {
        id: action.data.key,
        res: action.data.task_res[0]
    }
    // console.log("masterChangeWorkerTaskSaga is ", obj)
    try {
        const res = yield call(axios.post, API_URL + "master_manage/master_change_worker_task_res", {...obj})
        if (res.status == 200) {
            // 先提示，再刷新
            message.success('任务状态更改成功', duration=0.1).then(()=>{window.location.reload()})
        } else {
            message.error("请求失败")
        }
    } catch(e) {
        message.error("请求失败")
    }
}

// master 猎人向后端发送，删除任务，已完成、开发中的任务无法放弃
function* deleteMasterTaskSaga(action) {
    const obj = {
        id: action.data.key,
        res: action.data.task_res[0]
    }
    console.log("deleteMasterTaskSaga is ", obj)
    try{
        const res = yield call(axios.post, API_URL + "master_manage/delete_master_task", {...obj})
        if (res.status == 200) {
            // 先提示，再刷新
            message.success('删除成功', duration=0.1).then(()=>{window.location.reload()})
        } else {
            message.error("请求失败")
        }
    } catch(e) {
        message.error("请求失败")
    }
}

// master 向后端下载作证材料
function* downLoadTaskEviSaga(action) {
    console.log("downLoadTaskEviSaga is ", action.data)
    try{
        const url = API_URL + "master_manage/download_task_evi/" + action.data.id + "/" + action.data.email
        window.open(url)
        message.success("下载成功")
    }catch (e) {
        message.error("下载失败")
    }
}

// 雇主向后端发送修改任务请求
function* updateMasterTaskSaga(action) {
    const obj = yield select(state => state.TaskMasterManagePageReducer.updateTask)
    console.log("updateMasterTaskSaga is", obj)
    yield put(ChangeSpinACtivate({sendTask_activate: true}))
    try {
        const res = yield call (axios.post, API_URL + "master_manage/update_task_data", {...obj})
        if (res.status == 200) {
            yield put(ChangeSpinACtivate({sendTask_activate: false})) // 请求成功，加载状态清除
            yield put(ChangeMasterTasksDataActivate({activate: false})) // 关闭弹窗
            // 先提示，再刷新
            message.success('修改成功', duration=0.1).then(()=>{window.location.reload()})
        } else {
            message.error("修改失败")
            yield put(ChangeSpinACtivate({sendTask_activate: false})) // 请求成功，加载状态清除
        }
    } catch (e) {
        message.error("修改失败")
        yield put(ChangeSpinACtivate({sendTask_activate: false})) // 请求成功，加载状态清除
    }
    
}

export function* taskMasterManagePageSaga () {
    yield takeEvery(GETMASTERTASKSSAGA, getMasterTasksSaga)
    yield takeEvery(MASTERCHANGEWORKERTASKSAGA, masterChangeWorkerTaskSaga)
    yield takeEvery(DELETEMASTERTASKSAGA, deleteMasterTaskSaga)
    yield takeEvery(DOWNLOADTASKEVISAGA, downLoadTaskEviSaga)
    yield takeEvery(UPDATEMASTERTASKSAGA, updateMasterTaskSaga)
}
